<template>
  <!-- 车辆管理 -->
  <div class="CarManagement">
    <!-- 页面头部和查询区域 -->
    <div class="CarManagement-tittle">
      <div class="crumbs">
        <p><span>车行管理 /</span> <span> 车辆管理</span></p>
        <h3>车辆管理</h3>
      </div>
      <div class="search">
        <div class="search-left">
          <el-form-item label="绑定状态">
            <el-select clearable v-model="username" placeholder="请选择车位状态">
              <el-option label="全部" value="all" />
              <el-option label="审核中" value="pending" />
              <el-option label="审核通过" value="success" />
              <el-option label="审核不通过" value="error" />
            </el-select>
          </el-form-item>
          <el-form-item label="车牌号">
            <el-input clearable v-model="carnum" placeholder="请输入" />
          </el-form-item>
        </div>
        <div class="search-right">
          <el-button type="primary" @click="search">查询</el-button>
          <el-button type="primary" plain @click="reset">重置</el-button>
        </div>
      </div>
    </div>
    <!-- 新建按钮 -->
    <div class="CarManagement-content">
      <div class="content-title">
        <span>车辆列表</span>
        <div class="title-btn">
          <el-button type="primary" @click="go">+ 新建</el-button>
          <el-button type="primary" plain>批量操作</el-button>
        </div>
      </div>
      <!-- 表格 -->
      <el-table
        :data="tableData"
        stripe
        style="width: 100%"
        :cell-style="cellStyle"
        :header-row-style="headerStyle"
      >
        <el-table-column type="selection" />
        <el-table-column prop="carNum" label="车牌号" align="center" />
        <el-table-column prop="carType.carType" label="车辆品牌" align="center" />
        <el-table-column prop="model" label="车辆型号" align="center" />
        <el-table-column prop="carColor" label="车身颜色" align="center" />
        <el-table-column prop="person.username" label="持有人" align="center" />
        <el-table-column prop="remark" label="备注" align="center" />
        <el-table-column label="操作" align="center">
          <template #default="scope">
            <el-button type="primary" text @click="handleEdit()">编辑</el-button>
            <el-button type="primary" text @click="handleDelete()">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="demo-pagination-block">
        <Page
          :total="total"
          :count="count"
          :currentPage="page"
          @handleCurrentChange="handleCurrentChange"
          @handleSizeChange="handleSizeChange"
        ></Page>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from "vue";
import Page from "../../components/page.vue";
import http from "../../utils/http";
import { ElMessage } from "element-plus";
import router from "../../router";

const cellStyle = ref({
  height: "60px",
  fontSize: "15px",
});
const headerStyle = ref({
  height: "60px",
  fontSize: "15px",
  color: "black",
});

let tableData = ref([]);
let username=ref('')
let carnum=ref('')
let total = ref(50);
let page = ref(1);
let count = ref(5);
let handleDelete = () => {};
let handleEdit = () => {};
let handleCurrentChange = (val) => {
  console.log(val)
  page.value = val;
  getData();
};
let handleSizeChange = (val) => {
  console.log(val)
  count.value = val;
  getData();
};

// 渲染函数
let getData = () => {
  http({
    url: "/car/list",
    params: {
      page: page.value,
      limit: count.value,
      username: username.value,
      carnum: carnum.value,
    },
  }).then(({ data }) => {
    console.log(data);
    if (data.code == 0) {
      tableData.value = data.data;
      total.value = data.total;
    }
  });
};
//搜索
let search = () => {
  getData();
};
//重置
let reset = () => {
  username.value = "";
  carnum.value = "";
  page.value = 1;
  getData();
};
// //删除
// let del = (index) => {
//   console.log(index);
//   http({
//     url: "/region/delete",
//     method: "DELETE",
//     params: { id: index },
//   }).then(({ data }) => {
//     // console.log(data)
//     if (data.code == 0) {
//       ElMessage({
//         message: "删除成功",
//         type: "success",
//       });
//       getData();
//     } else {
//       ElMessage.error("删除失败");
//     }
//   });
// };
//新建
let go=()=>{
  router.push("/home/NewCar");
}


onMounted(() => {
  console.log("挂载后");
  getData();
});
</script>

<style lang="less" scoped>
.CarManagement {
  width: 90%;
  // height: 100%;
  margin: auto;
  .CarManagement-tittle {
    margin-top: 20px;
    background-color: white;
    .crumbs {
      padding: 15px 20px;
      font-size: 16px;
      border-bottom: 1px solid #ccc;
      p {
        margin-bottom: 10px;
        span:nth-child(1) {
          color: #8d8d8d;
        }
        span:last-child {
          color: #000;
        }
      }
    }
    .search {
      width: 93%;
      padding: 15px 20px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .search-left {
        display: flex;
        div:nth-child(1){
          margin-right: 50px;
        }
        .el-input {
          margin-left: 15px;
          width: 250px;
        }
      }
    }
  }
  .CarManagement-content {
    padding: 15px 20px;
    margin-top: 20px;
    background-color: white;
    
    .content-title {
      width: 96%;
      padding-bottom: 20px;
      border-bottom: 1px solid #eee;
      display: flex;
      justify-content: space-between;
      align-items: center;
    }
  }
  .el-pagination {
    margin-top: 20px;
    // padding-right: 10px;
    justify-content: flex-end;
  }
}
</style>
